Imports pixolut.BizBlox
Imports System.Text

Public Class CSDORenderEngine
    Inherits CSAbstractRenderEngine

    Public Sub New(ByVal u As UpdateProgress, ByVal s As UpdateStatus)
        MyBase.New(u, s, GetType(SQL))
    End Sub

    Protected Overrides Function FormatFilename(ByVal tableName As String) As String
        Return tableName
    End Function

    Protected Overrides Function RenderPrefix(ByVal t As Table) As String
        Dim sb As New StringBuilder
        sb = sb.Append("using pixolut.BizBlox;").Append(vbCrLf).Append(vbCrLf)
        sb = sb.Append("public class ").Append(t.TableName)
        sb = sb.Append(" : DataObject").Append(vbCrLf)
        sb = sb.Append("{").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderPostfix(ByVal t As Table) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf)
        sb = sb.Append("}").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderColumn(ByVal t As Table, ByVal index As Integer) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf)
        Dim col As Column = t(index)
        sb = sb.Append("    private ").Append(col.DataType).Append(" m_").Append(col.ColumnName).Append(";").Append(vbCrLf)
        sb = sb.Append(Me.RenderAttributes(col.Attributes()))
        sb = sb.Append("    public ").Append(col.DataType).Append(" ").Append(col.ColumnName).Append("{").Append(vbCrLf)
        sb = sb.Append("        get {").Append(vbCrLf)
        sb = sb.Append("            return m_").Append(col.ColumnName).Append(";").Append(vbCrLf)
        sb = sb.Append("        }").Append(vbCrLf)
        sb = sb.Append("        set {").Append(vbCrLf)
        sb = sb.Append("            this.Touch((int)Columns." & col.ColumnName & ");").Append(vbCrLf)
        sb = sb.Append("            m_").Append(col.ColumnName).Append(" = value;").Append(vbCrLf)
        sb = sb.Append("        }").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderForeignColumn(ByVal t As Table, ByVal index As Integer) As String
        Dim sb As New StringBuilder
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public ").Append(t(index).FKTable).Append(" GetBy").Append(t(index).ColumnName).Append("() {").Append(vbCrLf)
        sb = sb.Append("        ").Append(t(index).FKTable).Append(" obj = new ").Append(t(index).FKTable).Append("(").Append(t(index).ColumnName).Append(");").Append(vbCrLf)
        sb = sb.Append("        obj.Load();").Append(vbCrLf)
        sb = sb.Append("        return obj;").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public ").Append(t(index).FKTable).Append(" GetBy").Append(t(index).ColumnName).Append("(params System.Enum[] columns) {").Append(vbCrLf)
        sb = sb.Append("        ").Append(t(index).FKTable).Append(" obj = new ").Append(t(index).FKTable).Append("(").Append(t(index).ColumnName).Append(");").Append(vbCrLf)
        sb = sb.Append("        obj.Load(columns);").Append(vbCrLf)
        sb = sb.Append("        return obj;").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        Return sb.ToString()
    End Function

    Protected Overrides Function RenderElements(ByVal t As Table) As String
        Dim sb As New StringBuilder
        If t.GenerateDefaultConstructor Then
            sb = sb.Append(vbCrLf)
            sb = sb.Append("    public ").Append(t.TableName).Append("(){").Append(vbCrLf)
            sb = sb.Append("    }").Append(vbCrLf)
        End If
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public ").Append(t.TableName).Append("(System.Guid uid) : base(uid){").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public ").Append(t.TableName).Append("(System.Guid uid, System.Boolean load) : base(uid, load){").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public ").Append(t.TableName).Append("(ref ").Append("DOCollection baseCol, int index) : base(ref baseCol, index){").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public ").Append(t.TableName).Append("(System.Data.DataRow dr, DOSelection[] selections) : base(dr, selections){").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public new enum Columns {").Append(vbCrLf)
        For j As Integer = 0 To t.ColumnCount - 1
            sb = sb.Append("        ").Append(t(j).ColumnName)
            If j < t.ColumnCount - 1 Then
                sb = sb.Append(",").Append(vbCrLf)
            Else
                sb = sb.Append(vbCrLf)
            End If
        Next
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public override string TableName() {").Append(vbCrLf)
        sb = sb.Append("        return """).Append(t.TableName).Append(""";").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        sb = sb.Append("    public override System.Enum UidColumn() {").Append(vbCrLf)
        sb = sb.Append("        return Columns.").Append(t.PKColumnName).Append(";").Append(vbCrLf)
        sb = sb.Append("    }").Append(vbCrLf)
        sb = sb.Append(vbCrLf)
        Return sb.ToString()
    End Function
End Class
